package com.blueheart.Test.lang.ref;

import java.lang.reflect.*;

public class RefTest {

    private int value;

    public RefTest() {}

    public RefTest(Integer value) { this.value = value;}

    private void hello(String name){
        System.out.println("hello , " + (name == null ? "world" : name));
    }

    public static void main(String[] args) throws NoSuchMethodException, IllegalAccessException, InvocationTargetException, InstantiationException, NoSuchFieldException {

        //1.反射方法构建对象
        Class<RefTest> clazz = RefTest.class;
        Constructor<RefTest> constructor = clazz.getConstructor(Integer.class);
        RefTest refTest = constructor.newInstance(100);
        System.out.println(refTest.value);

        //2.操作value
        Field[] declaredFields = clazz.getDeclaredFields();
        //
        Field value = clazz.getDeclaredField("value");
        if(!value.isAccessible()){
            value.setAccessible(true);
        }
        value.set(refTest,200);
        System.out.println(refTest.value);

        //3.操作方法
        Method declaredMethod = clazz.getDeclaredMethod("hello",String.class);
        if(!declaredMethod.isAccessible()){
            declaredMethod.setAccessible(true);
        }

        declaredMethod.invoke(refTest,"lzx");


        //Type
        Type refTestType = refTest.getClass();
        System.out.println(refTestType.getTypeName());
        Type intType = int.class;
        System.out.println(intType.getTypeName());
        Type intArrType = int[].class;
        System.out.println(intArrType);
    }

}
